package net.sf.cldsql.sql2json.sql;

import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.cldsql.dj.utl.DiException;
import net.sf.cldsql.dj.utl.StrUtl;
import net.sf.cldsql.dj.utl.XfixParser;

/**
 * SqlQuery
 */
public class SqlRestQuery extends SqlBaseQuery {
    static final XfixParser suffix = new XfixParser(XfixParser.SUFFIX, ":");
    static final StrUtl sutl = new StrUtl();
    
    final static Logger log = LoggerFactory.getLogger(SqlRestQuery.class);
    
    public void parseRequest(String tableName, String idName, String idValue, String method, Map<String,String[]> params) {
        log.debug("parseRequest start table={} idName={} idValue{} method={} paramMap={}", tableName, method, idName, idValue, params);
        this.tableName = sutl.convLJavaToSql(tableName);
        if (method.equals("GET")) {
            stmtType = SELECT;
            if (idName != null && idValue != null) {
                //whereList.add (new AndEntry(sutl.convLJavaToSql(idName), "=", idValue));
                addWhere(idName, idValue);
            } else {
                for (String key : params.keySet()) {
                    //whereList.add (new AndEntry(sutl.convLJavaToSql(key), "=", params.get(key)[0]));
                    addWhere(key, params.get(key)[0]);
                }
            }
        } else if (method.equals("POST")) {
            stmtType = UPDATE;
            if (idName != null && idValue != null) {
                //whereList.add (new AndEntry(sutl.convLJavaToSql(idName), "=", idValue));
                addWhere(idName, idValue);
                for (String key : params.keySet()) {
                    updateList.add (new AndEntry(sutl.convLJavaToSql(key), "=", params.get(key)[0]));
                }
            } else {
                new DiException("ID is necessary");
            }
        } else if (method.equals("PUT")) {
            stmtType = INSERT;
            if (idValue != null) {
                new DiException("Cannot insert with ID");
            }
            for (String key : params.keySet()) {
                updateList.add (new AndEntry(sutl.convLJavaToSql(key), "=", params.get(key)[0]));
            }
        } else if (method.equals("DELETE")) {
            stmtType = DELETE;
            if (idName != null && idValue != null) {
                //whereList.add (new AndEntry(sutl.convLJavaToSql(idName), "=", idValue));
                addWhere(idName, idValue);
            } else {
                new DiException("Should only specify ID");
            }
        } else {
            new DiException("Unknown method " + method);
        }

    }
    
    protected void addWhere (String name, String value) {
        String key = name;
        String op = null;
        String clause = null;
        String func = null;
        
        if (suffix.parse(key, "orderBy")) {
            clause = "orderBy";
            orderByList.add(new AndEntry(sutl.convLJavaToSql(suffix.getOtherPart()), "=", value.isEmpty() ? "asc" : value));
        //} else if (suffix.parse(key, "groupBy")) {
        //    clause = "groupBy";
        //    groupByList.add(new AndEntry(sutl.convLJavaToSql(suffix.getOtherPart()), "", ""));
        } else if (suffix.parse(key, "limit")) {
            clause = "limit";
            limit = Long.parseLong(value);
        } else {
            if (suffix.parse(key, "eq")) {
                op = "=";
            } else if (suffix.parse(key, "gt")) {
                op = ">";
            } else if (suffix.parse(key, "gte")) {
                op = ">=";
            } else if (suffix.parse(key, "lt")) {
                op = "<";
            } else if (suffix.parse(key, "lte")) {
                op = "<=";
            } else if (suffix.parse(key, "neq")) {
                op = "!=";
            } else if (suffix.parse(key, "like")) {
                op = "like";
            } else if (suffix.parse(key, "lpr")) {
                op = "(";
            } else if (suffix.parse(key, "rpr")) {
                op = ")";
            } else if (suffix.parse(key, "and")) {
                op = "and";
            } else if (suffix.parse(key, "or")) {
                op = "or";
            } else if (suffix.parse(key, "in")) {
                op = "in";
            }
            
            if (op != null) {
                String column = sutl.convLJavaToSql(suffix.getOtherPart());
                whereList.add(new AndEntry (column, op, value)); 
            } else {
                op = "=";
                String column = sutl.convLJavaToSql(key);
                whereList.add(new AndEntry (column, op, value)); 
            }
        }
    }

}
